package com.example.teatea.controller;

import com.example.teatea.LoginApp;
import com.example.teatea.dao.AddStaff;
import com.example.teatea.dao.CheckRegister;
import com.example.teatea.dao.UpdatePsw;
import com.example.teatea.tools.MD5;
import com.example.teatea.tools.VerifyUtil;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import static com.example.teatea.controller.RegisterController.getRandom;
import static com.example.teatea.controller.RegisterController.s;

public class FindPswConroller implements Initializable {
    public TextField phone;
    public TextField code;
    public PasswordField password;
    public Button findBt;
    public Button returnBt;
    public ImageView verify;

    public void toFind(ActionEvent actionEvent) throws Exception {
        if(phone.getText().equals("")){
            JOptionPane.showMessageDialog(null,"手机号不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(code.getText().equals("")){
            JOptionPane.showMessageDialog(null,"验证码不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(!s.equals(code.getText())){
            JOptionPane.showMessageDialog(null,"验证码错误！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(password.getText().equals("")){
            JOptionPane.showMessageDialog(null,"密码不能为空！","提示",JOptionPane.ERROR_MESSAGE);
        }else if(!new CheckRegister().checkRegister(phone.getText())){
            JOptionPane.showMessageDialog(null,"该用户不存在！","提示",JOptionPane.ERROR_MESSAGE);
        }else{
            MD5 md5 = new MD5();
            boolean flag = new UpdatePsw().updatePsw(phone.getText(),md5.generateMD5(password.getText()));
            if(flag){
                JOptionPane.showMessageDialog(null, "恭喜你，修改成功!", "提示",JOptionPane.PLAIN_MESSAGE);
                Stage stage = (Stage)returnBt.getScene().getWindow();
                stage.close();
                LoginApp loginApp = new LoginApp();
                loginApp.start(stage);
            }else{
                JOptionPane.showMessageDialog(null, "修改失败", "提示",JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void toReturn(ActionEvent actionEvent) throws Exception {
        Stage stage = (Stage)returnBt.getScene().getWindow();
        stage.close();
        LoginApp loginApp = new LoginApp();
        loginApp.start(stage);
    }

    public void toFresh(MouseEvent mouseEvent) {
        FileOutputStream out = null;
        try{
            out = new FileOutputStream("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\1.png");
            VerifyUtil.generateVerify(out,getRandom(4));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Image image = new Image("file:E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\1.png");
        verify.setImage(image);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        FileOutputStream out = null;
        try{
            out = new FileOutputStream("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\1.png");
            VerifyUtil.generateVerify(out,getRandom(4));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Image image = new Image("file:E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\1.png");
        verify.setImage(image);
    }
}
